﻿using JinJun.YShow.BLL;
using JinJun.YShow.BLL.IService;
using JinJun.YShow.Model;
using JinJun.YShow.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;

namespace JinJun.YShow.Web.Controllers
{
    public class OrderController : BaseController
    {
        

        private IOrderService _orderService;
       

        public OrderController(IOrderService orderService)
        {
            _orderService = orderService;
        }


        /// <summary>
        /// 订单分页查询
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        /// <returns></returns>
        public JsonResult GetOrderList(int page, int limit,string adddate, string enddate)
        {
            //创建分页类对象
            PageListResult<OrderQuery> pageListResult = new PageListResult<OrderQuery>();          
            //调用订单分页查询
            List<OrderQuery> orderQueries = _orderService.GetOrderList(adddate,enddate);
            //赋值
            pageListResult.code = 0;
            pageListResult.msg = string.Empty;
            pageListResult.data = orderQueries.Skip((page - 1) * limit).Take(limit).ToList();
            pageListResult.count = orderQueries.Count;
            return Json(pageListResult, JsonRequestBehavior.AllowGet);
        }


        /// <summary>
        /// 批量删除根据Id
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        /// <returns></returns>
        public JsonResult BatchDeleteById(string id)
        {
            //用string类型数组去接收所传参数拆分后的值
            string[] num1 = id.Split(',');
            //将num1中的值用int类型数组去接收
            int[] num2 = new int[num1.Length];
            for (int i = 0; i < num2.Length; i++)
            {   
                num2[i] = Convert.ToInt32(num1[i]);
            }
            //定义List集合
            List<OrderInfo> orderinfoList = new List<OrderInfo>();
            List<Order_Detail> orderdetailList = new List<Order_Detail>();
            BaseService<Order_Detail> _orderdetailbaseService = new BaseService<Order_Detail>();
            //循环获取对象
            for (int i = 0; i < num2.Length; i++)
            {
                int num = num2[i];
                //定义查询拉姆达表达式
                Expression<Func<OrderInfo, bool>> orderinfoLambda = a => a.Order_Id ==num;
                Expression<Func<Order_Detail, bool>> orderdetailLambda = a => a.Order_Id == num;
                //循环查出每一个对象存入List集合
                orderinfoList.Add(_orderService.Query(orderinfoLambda));
                orderdetailList.Add(_orderdetailbaseService.Query(orderdetailLambda));
            }
            //创建操作类对象 
            OperateResult operateResult = new OperateResult();          
            //调用批量删除方法
            operateResult.Success = _orderService.BatchDeleteById(orderinfoList, orderdetailList);
            return Json(operateResult, JsonRequestBehavior.AllowGet);
        }



        /// <summary>
        /// 删除功能
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        /// <returns></returns>
        public JsonResult Delete(int id)
        {
            //创建操作类对象 
            OperateResult operateResult = new OperateResult();
            //创建业务层对象
            BaseService<OrderInfo> orderinfo = new BaseService<OrderInfo>();
            BaseService<Order_Detail> orderdetail = new BaseService<Order_Detail>();        
            //定义查询拉姆达表达式
            Expression<Func<OrderInfo, bool>> orderinfoLambda = a => a.Order_Id == id;
            Expression<Func<Order_Detail, bool>> orderdetailLambda = a => a.Order_Id == id; 
            //调用查询返回一个对象
            OrderInfo orderinfoList = orderinfo.Query(orderinfoLambda);
            Order_Detail orderdetailList = orderdetail.Query(orderdetailLambda);   
            //调用删除方法
            operateResult.Success = _orderService.Delete(orderinfoList, orderdetailList);
            return Json(operateResult);
        }


        /// <summary>
        /// 修改发货状态
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        /// <returns></returns>
        public JsonResult UpdateStatus(OrderQuery orderQuery)
        {
            //创建操作类对象 
            OperateResult operateResult = new OperateResult();
            //定义查询拉姆达表达式
            Expression<Func<OrderInfo, bool>> orderinfoLambda = a => a.Order_Id == orderQuery.Order_Id;
            orderQuery.CreateTime = DateTime.Now;
            //调用查询返回一个对象
            OrderInfo orderinfoList = _orderService.Query(orderinfoLambda);
            //修改状态
            orderinfoList.Order_Status = Convert.ToByte(orderQuery.Order_Status);
            //调用修改方法
            operateResult.Success = _orderService.Update(orderinfoList);
            return Json(operateResult, JsonRequestBehavior.AllowGet);
        }


        /// <summary>
        /// 编辑订单信息
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="roleId"></param>
        /// <param name="moduleId"></param>
        /// <returns></returns>
        public JsonResult EditOrderInfo(OrderQuery orderQuery)
        {
            //创建操作类对象 
            OperateResult operateResult = new OperateResult();
            //创建业务层对象         
            BaseService<Order_Detail> orderdetail = new BaseService<Order_Detail>();
            //定义查询拉姆达表达式
            Expression<Func<OrderInfo, bool>> orderinfoLambda = a => a.Order_Id == orderQuery.Order_Id;
            Expression<Func<Order_Detail, bool>> orderdetailLambda = a => a.Order_Id == orderQuery.Order_Id;
            orderQuery.CreateTime = DateTime.Now;
            //调用查询返回一个对象
            OrderInfo orderinfoList = _orderService.Query(orderinfoLambda);
            Order_Detail orderdetailList = orderdetail.Query(orderdetailLambda);
            //将查询到的值赋给自定义实体类中的值
            orderinfoList.Order_No = orderQuery.Order_No;
            orderdetailList.Product_Name = orderQuery.Product_Name;
            orderinfoList.Order_Money = orderQuery.Order_Money;
            orderinfoList.District_Money = orderQuery.District_Money;
            orderinfoList.CreateTime = orderQuery.CreateTime;
            orderinfoList.Order_Status = Convert.ToByte(orderQuery.Order_Status);
            orderdetailList.Product_Cnt = orderQuery.Product_Cnt;
            //调用修改方法
            operateResult.Success = _orderService.EditOrderInfo(orderinfoList,orderdetailList);
            return Json(operateResult, JsonRequestBehavior.AllowGet);
        }



        /// <summary>
        /// 交易统计--折线图
        /// </summary>
        /// <returns></returns>
        public JsonResult QueryInfo()
        {
            //查询表达式
            Expression<Func<OrderInfo, bool>> orderinfoLambda = a => true;
            //调用查询方法返回一个集合
            List<OrderInfo> orderInfos = _orderService.QueryList(orderinfoLambda);
            //按日期进行分组来得到每个时间段的交易金额和订单数量并转化为List集合
            var q = orderInfos.GroupBy(a => a.Shipping_Time.Value.Date).Select(a => new
            {
                count = a.Count(),
                money = a.Sum(b=>b.Order_Money)

            }).ToList();
            //定义交易金额数组
            int[] ids = new int[q.Count];
            //定义订单数量数组
            int[] idss = new int[q.Count];
            //循环遍历
            for (int i = 0; i < q.Count; i++)
            {
                ids[i] = Convert.ToInt32(q[i].money);
                idss[i] = q[i].count;
            }
            //创建以int类型的数组为对象集合
            List<int[]> vs = new List<int[]>();
            //将交易金额添加到集合
            vs.Add(ids);
            //将订单数量添加到集合
            vs.Add(idss);
            return Json(vs, JsonRequestBehavior.AllowGet);
        }
    }
}